package com.zdd.chap5b.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdd.chap5b.dao.BookMapper;
import com.zdd.chap5b.entity.Book;
import com.zdd.chap5b.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IBookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    BookMapper bookMapper;
    @Override
    public List<Book> searchBooks(Book book) {
    /*
        //使用QueryWrapper封装查询条件
        QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(book.getName()!=""&&book.getName()!=null,"name",book.getName());
        queryWrapper.eq(book.getCategory()!=""&&book.getCategory()!=null,"category",book.getCategory());
        queryWrapper.eq(book.getAuthor()!=""&&book.getAuthor()!=null,"author",book.getAuthor());
        return bookMapper.selectList(queryWrapper);
     */
        //使用LambdaQueryWrapper封装查询条件
        LambdaQueryWrapper<Book> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(book.getName()!=""&&book.getName()!=null,Book::getName,book.getName());
        queryWrapper.eq(book.getCategory()!=""&&book.getCategory()!=null,Book::getCategory,book.getCategory());
        queryWrapper.eq(book.getAuthor()!=""&&book.getAuthor()!=null,Book::getAuthor,book.getAuthor());
        return bookMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<Book> getPage(int pageNum, int size) {
        //参数一是当前页，参数二是每页的个数
        IPage<Book> bookIPage=new Page<>(pageNum,size);
        bookIPage=bookMapper.selectPage(bookIPage,null);
        return bookIPage;
    }
}
